
const startKuaidi = async (id, code) => {
  if(!window.utools)return[];
  let odd,phone;
  let bool = /SF\d+$/.test(id);
  if(bool){
    odd = id.slice(0, -4);
    phone = id.slice(-4);
  }else{
    odd = id;
    phone = '';
  }
  let res = await window.utools.ubrowser
    .goto("https://www.kuaidi100.com/")
    // .devTools('right')
    .click(code ? `a[data-code="${code}"]` : `a[data-code]`)
    .value("#input", odd)
    .click("#query")
    .wait((phone)=>{
      let bool = document.querySelector('.check-code-box')
      if(bool){
        let inputs = document.querySelectorAll('.check-code-box input')
        inputs.forEach((item,index)=>{
          item.value = phone[index]
        })
        document.querySelector('.check-code-box .btn').click();
        return true;
      }else{
        return true;
      }
    }, 3000, phone)
    .wait(".info-item.comname", 60000)
    .evaluate(() => {
      let title = document.querySelector(".info-item.comname").innerText;
      let listEl = document.querySelectorAll(".result-body .content .step");
      if(!title || !listEl){return false}
      let arr = [...listEl].map((item) => {
        let state = item.classList.contains("start");
        let time = item.querySelector(".datetime").innerText;
        let info = item.querySelector(".text").innerText;
        return {
          time,
          info,
          state,
        };
      });
      return {
        title,
        list: arr,
      };
    })
    .run({ show: false });
    // .run({ width: 1300, height: 1000 });
    window.utools.clearUBrowserCache();
  return res[0]
};

export { startKuaidi };
